package sicau.practice.community.utils;

import com.baidu.aip.imageclassify.AipImageClassify;
import org.json.JSONObject;
import sicau.practice.community.dto.Car;

import java.util.HashMap;

public class BaiduAI {



    public static final String APP_ID = "17864759";
    public static final String API_KEY = "CG4kOpn8VC7Rwcg67PvcQCW5";
    public static final String SECRET_KEY = "AMThbEMQQwaOTebGih7T73fcg4mZbaOU";

    public Car getResult(){

        // 初始化一个AipImageClassify
        AipImageClassify client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);


        HashMap<String, String> options = new HashMap<String, String>();
        options.put("top_num", "3");
        options.put("baike_num", "5");

        // 参数为本地路径
        String image = "F:\\\\shixun\\\\pro\\\\community\\\\src\\\\main\\\\resources\\\\static\\\\img\\\\car.jpg";
        JSONObject res = client.carDetect(image, options);

        String resultStr = res.toString(2);

        //获取车型名字
        int np = resultStr.indexOf("name");
        String  c = resultStr.substring(np+8,np+30);
        int nend = c.indexOf("\"");
        String carname = c.substring(0,nend);

        //获取车型描述
        int nd = resultStr.indexOf("description");
        String  d = resultStr.substring(nd+15,nd+1000);
        int dend = d.indexOf("\"");
        String description = d.substring(0,dend);

        Car car = new Car();
        car.setName(carname);
        car.setDescription(description);
        return car;



    }

}